using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using mvLibrary.Helpers.Utils;
using mvLibrary.Helpers.Utils;

namespace fenk.Varios
{
    public partial class CallCenter : mvLibrary.BaseForm
    {
        public CallCenter()
        {
            InitializeComponent();
        }
        // AL LLAMAR AL PROGRAMA "CALL.CENTER" APLIC.DATA ENVIA ESTOS VALORES..
        // ITEM<20> = FECHA 
        //
        // APLIC.DATA<1> "LIST" carga listado de llamados pendientes
        // regresando...
        // ITEM<1,MV> = CALL ID
        // ITEM<2,MV> = HORA
        // ITEM<3,MV> = DETALLE
        // ITEM<4,MV> = USER
        // ITEM<5,MV> = INTERNO

        // o..
        // APLIC.DATA<1> = "UPDATE" actualiza llamado
        // APLIC.DATA<2> = MV - posicion del llamado en ITEM
        // APLIC.DATA<3> = RESULTADO: 'CUMPLIDA','CANCELADA','TRANSFERIDA'
        // APLIC.DATA<4> = FECHA TRANSFERIDA
        // APLIC.DATA<5> = HORA TRANSFERIDA
        //
        public void UpdateGrid()
        {
            callsGrid.Rows.Clear();
            int max = UniItem.Dcount(1);
            for (int i = 0; i < max; i++)
			{
			 callsGrid.Rows.Add(UniItem.ExtractAsString(4,i+1),UniItem.ExtractAsString(5,i+1),UniAccount.Oconv(UniItem.ExtractAsString(2,i+1),"MT"),UniItem.ExtractAsString(3,i+1));
			}
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
           AplicData.Replace(1, "LIST");
           CallProgram("CALL.CENTER", 1, 1);
           UpdateGrid();
        }

       
        public override void AfterReadItem()
        {
        }

        
        private void CallCenter_Load(object sender, EventArgs e)
        {
            toolBox.Visible = false;
            mvTextID.Text = DateTime.Now.ToShortDateString();
            AplicData.Replace(1,"LIST");
            CallProgram("CALL.CENTER",1,1);
            UpdateGrid();
        }

    

        private void button2_Click(object sender, EventArgs e)
        {
            AplicData.Replace(1, "LIST");
            CallProgram("CALL.CENTER",1,1);
            if (UniItem.Dcount(1) == 0)
            {
                MessageBox.Show("No hay llamados pendientes");
                mvTextID.Focus();
            }
            else
            {
                UpdateGrid();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (textSearch.Text.Length == 0)
            {
                MessageBox.Show("Falta indicar texto de busqueda", "Buscar en Agenda");
                textSearch.Focus();
                return;
            }
            AplicData.Replace(1,"SEARCH");
            AplicData.Replace(2, textSearch.Text);
            CallProgram("CALL.CENTER", 1, 1);
            if (AplicData.Dcount(3) == 0)
            {
                MessageBox.Show("No hay registros");
                textSearch.Focus();
            }
            {
                gridAgenda.Rows.Clear();
                int max = AplicData.Dcount(3);
                for (int i = 0; i < max; i++)
                {
                    gridAgenda.Rows.Add(AplicData.ExtractAsString(3, i + 1), AplicData.ExtractAsString(4, i + 1), AplicData.ExtractAsString(5, i + 1), AplicData.ExtractAsString(6, i + 1));
                }
            }
        }

        private void callsGrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string date = UniAccount.Oconv(UniItem.ExtractAsString(20), "D2/");
            string time = UniAccount.Oconv(UniItem.ExtractAsString(2), "MTS");

            string callDate = date + " " + time;
            fenk.Varios.CallCenterCloseCall form = new CallCenterCloseCall(callDate,UniItem.ExtractAsString(4,e.RowIndex + 1),UniItem.ExtractAsString(3, e.RowIndex + 1));
            form.ShowDialog();
            if (form.DialogResult == DialogResult.OK)
            {
                AplicData.Replace(1, "UPDATE");
                AplicData.Replace(2,e.RowIndex.ToString());
                AplicData.Replace(3, form.Resultado);
                AplicData.Replace(4, form.Notas);
                AplicData.Replace(5, form.Fecha);
                AplicData.Replace(6, form.Hora);
                CallProgram("CALL.CENTER",1,1);
                UpdateGrid();
            }
          
        }
        
    }
}

